package com.itheima.crm.controller;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.itheima.common.utils.Page;
import com.itheima.crm.pojo.BaseDict;
import com.itheima.crm.pojo.Organization;
import com.itheima.crm.pojo.RiZhi;

import com.itheima.crm.pojo.YuanGong;
import com.itheima.crm.service.BaseDictService;

import com.itheima.crm.service.OrganizationService;
import com.itheima.crm.service.RiZhiService;
import com.itheima.crm.service.YuanGongService;

/**
 * 客户管理
 * 
 * @author lx
 *
 */
@Controller
public class GuanLIController {

	@Autowired
	private BaseDictService baseDictService;
	@Autowired
	private YuanGongService yuangongService;
	@Autowired
	private RiZhiService rizhiService;
	@Autowired
	private OrganizationService organizationService;
	
	
	// 注解在成员变量上

	public BaseDictService getBaseDictService() {
		return baseDictService;
	}



	public void setBaseDictService(BaseDictService baseDictService) {
		this.baseDictService = baseDictService;
	}



	public YuanGongService getYuangongService() {
		return yuangongService;
	}



	public void setYuangongService(YuanGongService yuangongService) {
		this.yuangongService = yuangongService;
	}



	public RiZhiService getRizhiService() {
		return rizhiService;
	}



	public void setRizhiService(RiZhiService rizhiService) {
		this.rizhiService = rizhiService;
	}



	public OrganizationService getOrganizationService() {
		return organizationService;
	}



	public void setOrganizationService(OrganizationService organizationService) {
		this.organizationService = organizationService;
	}



	// 入口
	@RequestMapping(value = "/QuanXian")
	public String QuanXian(YuanGong vo, Model model,HttpServletRequest request) {
		HttpSession session=request.getSession(false);
		String zhiwei = (String) session.getAttribute("zhiwei");
		
		if (zhiwei.equals("3")) {
		Integer id=yuangongService.MaxId()+1;
		
		System.out.println(id);
		request.getSession().setAttribute("yuangong_ygidinsert",id);
		
		List<BaseDict> SexType = baseDictService.selectBaseDictListByCode("001");
		List<BaseDict> OrganizationType = baseDictService.selectBaseDictListByCode("002");
		List<BaseDict> zhiweiType = baseDictService.selectBaseDictListByCode("003");
	
		
		model.addAttribute("SexType", SexType);
		model.addAttribute("OrganizationType", OrganizationType);
		model.addAttribute("zhiweiType", zhiweiType);

		// 通过四个条件 查询分页对象
		
		Page<YuanGong> page = yuangongService.selectPageByQueryVo(vo);
		
		model.addAttribute("page",page);
		
		
		return "QuanXian";
		}
		return null;
		}
	
	
	
	// 修改保存
	@RequestMapping(value = "/QuanXian/update.action")
	public @ResponseBody  JSONObject update(YuanGong yuangong) {
		RiZhi a=new RiZhi();
		a.setYgid(yuangong.getYgid());
		a.setYgname(yuangong.getYgname());
		a.setInsertdate(new Date());
		System.out.println(a.getInsertdate());
		a.setDetial("修改员工权限");
		rizhiService.insertApplication(a);
		
		
		
		
		System.out.println(yuangong.toString());
		JSONObject obj=new JSONObject();
		
		//通过部门，职位查数据，如果重复，返回取消
		  List<YuanGong> y=yuangongService.zhiweichongfu(yuangong);  
		  
		  
		  
		 if(null==y||y.size()==0) {
			 System.out.println("=========================================");
			 if(yuangong.getZhiwei().equals("2")) {
				  Organization newO=new Organization();
				  newO.setOname(yuangong.getOrganization());
				  newO.setOpeople(yuangong.getYgname());
				  System.out.println(newO.toString());
				 organizationService.updateManager(newO);
				 System.out.println("修改成功");
				 
			 }
				yuangongService.changeQuanXian(yuangong);
				obj.put("message","OK");
				
			 
		 } 
		 else {
			 System.out.println("+++++++++++++++++++++++++++++++++++++");
		 String data="";
			 for(int i=0;i<y.size();i++) {
				 YuanGong c=y.get(i);
				 String zhiwei=c.getZhiwei();
				 
				 if(zhiwei.equals("1")) {
					 yuangongService.changeQuanXian(yuangong);
					
					 obj.put("message","OK");
					 return obj;
				 }
				 
				
				 String organization=c.getOrganization();
				 String ygname=c.getYgname();
				 data+= organization+"."+ygname+"为"+zhiwei+",请先改变其职位";
				 
				 System.out.println(data);
			 }
			 	
			 	obj.put("message",data);
				
		 }
		 return obj;
		 }
		
		
		
		
	
	
	// 去修改页面
	@RequestMapping(value = "/QuanXian/edit.action")
	public @ResponseBody YuanGong edit(Integer id) {

		return yuangongService.selectYuanGongById(id);
	}
		
	
	
	@RequestMapping(value = "/GuanLiYuan")
	public ModelAndView GuanLiYuan(YuanGong yuangong, Model model, HttpServletRequest request) {

		YuanGong result = yuangongService.selectLogin(yuangong);

		if (yuangong.getLoginname().equals(result.getLoginname())
				&& yuangong.getLoginpw().equals(result.getLoginpw())&&result.getZhiwei().equals("3")) {
			System.out.println("相同");
			ModelAndView a = new ModelAndView();
			request.getSession().setAttribute("loginname", yuangong.getLoginname());
			request.getSession().setAttribute("loginpw", yuangong.getLoginpw());
			request.getSession().setAttribute("ygname", result.getYgname());
			request.getSession().setAttribute("YGid", result.getYgid());
			request.getSession().setAttribute("zhiwei", result.getZhiwei());
			request.getSession().setAttribute("organziation", result.getOrganization());
			a.setViewName("Main");
			
			return a;

		}

		ModelAndView a = new ModelAndView("login");

		return a;

	}
	
	@RequestMapping(value = "/RiZhi")
	public String RiZhi(RiZhi vo, Model model, HttpServletRequest request) {
		HttpSession session=request.getSession(false);
		String zhiwei = (String) session.getAttribute("zhiwei");
		
		if (zhiwei.equals("3")) {
		Page<RiZhi> page = rizhiService.selectApplicationList(vo);
		
		model.addAttribute("page",page);
		
		
		return "RiZhi";
		}
		return null;

	}
	
	


	
	
	


}
